﻿using System;
using System.Collections.Generic;
using System.Linq;
using Client.ViewModels;
using Contracts.Entities;

namespace Client.Commands
{
    public class AddActorCommand : BaseCommand
    {
        public override bool CanExecute(object parameter)
        {
            return !String.IsNullOrEmpty(parameter as string) &&
                   MainViewModel.Default.ActiveWorkspace.MovieItem != null &&
                   !MainViewModel.Default.ActiveWorkspace.MovieItem.Actors.Any(m => m.Name == parameter as string);
        }

        public override void Execute(object parameter)
        {
            var item = MainViewModel.Default.ActiveWorkspace.MovieItem;
            var newActor = new MovieElement()
            {
                Name = parameter as string,
                Type = MovieElementType.Actor
            };

            var actors = new List<MovieElement>(MainViewModel.Default.ActiveWorkspace.MovieItem.Actors);
            actors.Add(newActor);
            item.Actors = actors;

            MainViewModel.Default.ActiveWorkspace.MovieItem = item;
        }
    }
}
